#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   977.有序数组的平方.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/2 20:43
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""

# import lib
from typing import List


class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        result = [0 for _ in nums]
        l, r = 0, len(nums) - 1
        p = len(nums) - 1
        while l <= r:
            if (nums[l] * nums[l]) < (nums[r] * nums[r]):
                result[p] = nums[r] ** 2
                r -= 1
            else:
                result[p] = nums[l] ** 2
                l += 1
            p -= 1
        return result
